package com.merlin.demo_wellshape.repository;

import com.merlin.demo_wellshape.pojo.CheckInRecord;
import com.merlin.demo_wellshape.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

public interface CheckInRecordRepository extends JpaRepository<CheckInRecord, Integer> {

    List<CheckInRecord> findByUserAndCheckInDateBetween(User user, LocalDate startDate, LocalDate endDate);

    @Query("SELECT c FROM CheckInRecord c WHERE c.user.userId = :userId AND c.user.userPhone = :userPhone")
    List<CheckInRecord> findByUserIdAndUserPhone(@Param("userId") int userId, @Param("userPhone") String userPhone);


    /**
     * 判断用户今天是否签到
     *
     * @param user
     * @param checkInDate
     * @author Merlin
     */
    boolean existsByUserAndCheckInDate(User user, LocalDate checkInDate);

    /**
     * 根据用户和签到日期查询签到记录（删除签到记录会用到）
     * */
    @Query("SELECT c FROM CheckInRecord c WHERE c.user = :user AND c.checkInDate = :checkInDate")
    CheckInRecord findByUserAndCheckInDate(@Param("user") User user, @Param("checkInDate") LocalDate checkInDate);
}
